package servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import entity.User;
import service.ISmsserivce;
import service.impl.SmsserivceImpl;

/**
 * Servlet implementation class RegisterServlet
 */
@WebServlet(asyncSupported = true, urlPatterns = { "/RegisterServlet" })
public class RegisterServlet extends HttpServlet {

	ISmsserivce smsserivce = new SmsserivceImpl();
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html; charset=UTF-8");
		
		String uname = request.getParameter("uname");
		String upassword = request.getParameter("upassword");
		String realname = request.getParameter("realname");
		String role = request.getParameter("role");
		
		User user = new User(uname,upassword,realname,role);
		int addUser = smsserivce.addUser(user);
		
		System.out.println(addUser);
		if(addUser > 0) {
			
			response.getWriter().write("<script> alert('注册成功');location.href='login.jsp'</script>");

		}else {
			
			response.getWriter().write("<script>alert('注册失败！');location.href='login.jsp'</script>");
			
		}
	}

	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
